﻿@{
    var window = 7; // number of simultaneously displayed pages 
    var nextText = HasText(Model.NextText) ? Model.NextText : T(">").Text;
    var previousText = HasText(Model.PreviousText) ? Model.PreviousText : T("<").Text;

    var routeData = new RouteValueDictionary(ViewContext.RouteData.Values);
    var queryString = ViewContext.HttpContext.Request.QueryString;
    if (queryString != null) {
        foreach (string key in queryString.Keys) {
            if (key != null && !routeData.ContainsKey(key)) {
                var value = queryString[key];
                routeData[key] = queryString[key];
            }
        }
    }

    if (routeData.ContainsKey("id") && !HasText(routeData["id"])) {
        routeData.Remove("id");
    }

    var totalPageCount = (int)Math.Ceiling((double)Model.TotalItemCount / Model.PageSize);
    var firstPage = Math.Max(1, (int)Model.Page - (window / 2));
    var lastPage = Math.Min(totalPageCount, (int)Model.Page + (window / 2));

    Model.Classes.Add("pager");
    Model.Classes.Add("group");
    var tag = Tag(Model, "ul");

    if (Model.RouteData != null) {
        foreach (var rd in Model.RouteData.Values) {
            routeData[rd.Key] = rd.Value;
        }
    }

    var pageSizes = new List<int?>() { 10, 50, 100 };
    var defaultPageSize = WorkContext.CurrentSite.PageSize;
    if (!pageSizes.Contains(defaultPageSize)) {
        pageSizes.Add(defaultPageSize);
    }
}

@if (Model.TotalItemCount > 1) {
    <div class="pager-footer">

        @if (totalPageCount > 1 || Model.PageSize == 0 || Model.PageSize > pageSizes.First()) {
            <div class="page-size-options">
                <input type="hidden" name="Page" value="1" />
                <label for="pageSize">@T("Show:")</label>
                <select id="pageSize" name="PageSize">
                    @Html.SelectOption((int)Model.PageSize, 0, T("All").ToString())
                    @foreach (int size in pageSizes.OrderBy(p => p)) {
                        @Html.SelectOption((int)Model.PageSize, size, size.ToString())
                    }
                </select>

                <button id="submit-pager" type="submit">@T("Apply")</button>
            </div>
        }

        <span class="page-results">@T("Showing items {0} - {1} of {2}", (Model.Page - 1) * (int)Model.PageSize + 1, Model.PageSize == 0 ? Model.TotalItemCount : Math.Min(Model.TotalItemCount, (Model.Page) * (int)Model.PageSize), Model.TotalItemCount)</span>

        @if (totalPageCount > 1) {
            routeData["pageSize"] = Model.PageSize;
            @tag.StartElement

                // first
                if (firstPage > 1) {
                    if (routeData.ContainsKey("page")) {
                        routeData.Remove("page");
                    }

                    <li class="page-first">
                    @Html.ActionLink(T("<<").Text, (string)routeData["action"], (string)routeData["controller"], routeData, null)
                    </li>
                }

                // previous page
                if (Model.Page > 1) {
                    if (Model.Page == 2 && routeData.ContainsKey("page")) {
                        routeData.Remove("page");
                    } else {
                        routeData["page"] = Model.Page - 1;
                    }

                    <li class="page-previous">
                    @Html.ActionLink((string)previousText, (string)routeData["action"], (string)routeData["controller"], routeData, null)
                    </li>
                }

                // page numbers
                for (var p = firstPage; p <= lastPage; p++) {
                    <li class="page-@p">
                    @if (p == Model.Page) {
                        <span>@p</span>
                    } else {
                        if (p == 1) {
                            routeData.Remove("page");
                        } else {
                            routeData["page"] = p;
                        }
                        @Html.ActionLink(p.ToString(), (string)routeData["action"], (string)routeData["controller"], routeData, null)
                    }
                    </li>
                }

                // next page
                if (Model.Page < totalPageCount) {
                    routeData["page"] = Model.Page + 1;

                    <li class="page-next">
                    @Html.ActionLink((string)nextText, (string)routeData["action"], (string)routeData["controller"], routeData, null)
                    </li>
                }

                // last page
                if (lastPage < totalPageCount) {
                    routeData["page"] = totalPageCount;

                    <li class="page-last">
                    @Html.ActionLink(T(">>").Text, (string)routeData["action"], (string)routeData["controller"], routeData, null)
                    </li>
                }

            @tag.EndElement
        }

    </div>
}
@using(Script.Foot()) {
<script type="text/javascript">
//<![CDATA[
    $(function () {
        // Add the event handler for value change in the select field
        $("#pageSize").change(function () {
            var self = $(this);
            var form = self.closest("form");

            // Submit form
            form.submit();

            // disable button so that no other value can be chosen while the form is submited
            self.attr("disabled", true);
        });
    })
//]]>
</script>
}